/* NOTICE

Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 

Jim is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Jim is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

In addition, as a special exception, you are permitted to link this code
with the 3rd party libraries required to compile this file and to
distribute a linked combination including them. You must obey the
GNU General Public License in all respects for all of the code except for
those 3rd party libraries that are licenced under terms incompatible with the
GNU General Public License.  


NOTICE */

package au.com.lastweekend.jim.swing.folderchooser;

import java.io.File;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import au.com.lastweekend.jim.swing.LIFOListModel;
import au.com.lastweekend.jim.swing.WrappedComboBoxModel;
import au.com.lastweekend.jim.util.FolderUtil;

/**
 * A folder chooser that has a combobox.
 * 
 * Combination view/controller for this component.
 * 
 * @author ggardner
 * 
 */
public class ToolBarFolderChooser implements FolderChooser {

    private static final Logger LOG = Logger.getLogger(ToolBarFolderChooser.class);
    private JComboBox folderList = new JComboBox();

    private WrappedComboBoxModel<File> comboBoxModel;
    private LIFOListModel<File> listModel;

    public ToolBarFolderChooser(LIFOListModel<File> model) {

        this.listModel = model;
        this.comboBoxModel = new WrappedComboBoxModel<File>(model);

        folderList.setModel(comboBoxModel);
        folderList.setEditable(true);
    }

    public File chooseFolder(File currentDirectory) {

        File chosenDir;
        Object selectedItem = folderList.getSelectedItem();
        File selectedDir;
        // Selected will be a file or user entered String.
        if (selectedItem instanceof String) {
            String selectedString = (String) selectedItem;
            selectedDir = new File(selectedString);

        } else {
            selectedDir = (File) selectedItem;
        }
        chosenDir = selectedDir;
        if (chosenDir != null) {
            if (!chosenDir.isAbsolute()) {

                // Make it relative to whatever Jim believes is the current
                // directory.
                chosenDir = new File(currentDirectory, chosenDir.getPath());

            }

            chosenDir = FolderUtil.canonicalize(chosenDir);

            // TODO what if we've chosen something that is a file?
            if (!chosenDir.isDirectory()) {
                String displayPath = chosenDir.getAbsolutePath();

                if (!chosenDir.exists()
                        && JOptionPane.showConfirmDialog(folderList, displayPath, "Create Folder?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    if (!chosenDir.mkdirs()) {
                        LOG.warn("Failed to create folder: " + chosenDir.getAbsolutePath());
                        chosenDir = null;
                    } else {

                        LOG.info("Created folder " + chosenDir.getAbsolutePath());
                    }
                } else {
                    LOG.warn("Folder creation cancelled");
                    chosenDir = null;
                }

            }
        }
        if (chosenDir != null) {
            this.listModel.add(selectedDir);
            this.comboBoxModel.setSelectedItem(selectedDir);
        }

        return chosenDir;
    }

    public JComboBox getFolderList() {

        return folderList;
    }

}
